﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using MeTalker.Aide.Service;

namespace MeTalker.Aide.Forms
{
    internal partial class frmTopSession : MeForm
    {
        string appkey;
        string nick;
        string appSecret;
        internal IDictionary<string, string> Top_Session
        {
            get;
            set;
        }
        internal string ParamStr
        {
            get;
            set;
        }

        private frmTopSession()
        {
            InitializeComponent();
        }
        
        public frmTopSession(string nick, string appkey, string appSecret)
            : this()
        {
            this.appkey = appkey;
            this.appSecret = appSecret;
            this.nick = nick;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtSessionKey.Text == "")
            {
                MessageBox.Show("请将授权码粘贴到文本框中");
            }
            else if (txtSessionKey.Text.StartsWith("TOP") && txtSessionKey.Text.EndsWith("END"))
            {
                byte[] b = HttpClient.RequestHeart(string.Format(UtilHelper.TAOBAO_SESSION_API_URL, txtSessionKey.Text), new byte[1], 1);
                if (b != null)
                {
                    ParamStr = Encoding.UTF8.GetString(b);
                    if (!string.IsNullOrEmpty(ParamStr))
                    {
                        Top_Session = UtilHelper.GetParamTopSession(ParamStr);
                        if (Top_Session != null && Top_Session.Count > 3)
                        {
                            //if (Top_Session.ContainsKey(UtilHelper.TAOBAO_TOP_SESSION_STR))
                            //{
                            string sessionKey = Top_Session[UtilHelper.TAOBAO_TOP_SESSION_STR];
                            //string outNick;
                            //if (frmAddLicense.TestAccount(nick, appkey, appSecret, sessionKey, out outNick))
                            //{
                            if (!string.IsNullOrEmpty(sessionKey))
                            {
                                DialogResult = DialogResult.Yes;
                                this.Close();
                                return;
                            }
                            //}
                            //else if (!string.IsNullOrEmpty(nick))
                            //{
                            //    MeMessageBox.ShowInformation("您所输入的Appkey和AppSecret对应的授权不是" + nick + "，而是" + outNick + "的");
                            //    return;
                            //}
                            //}
                        }
                    }
                }
                MeMessageBox.ShowInformation("获取SSK错误!");
            }
        }

        private void lblRunCode_LinkClicked(object sender, EventArgs e)
        {
            try
            {
                Process.Start(string.Format(UtilHelper.TAOBAO_CODE_API_URL, appkey));
            }
            catch { }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            UtilHelper.RunBrowser(UtilHelper.HELP_BASE_URL + "57.html");
        }

    }
}
